/**
 * 基于信道号的手动互调干扰计算
 */
package com.gsmr.util;

/**
 * @author dell
 *
 */
public class calintermodu_base {
public calintermodu_base() {
        
    }
    //两点上行
    public String getPossibleInfChannel(int channel1, int channel2,int infchannel) {
        String result ="";
        double freq1 = getCenterFreq(channel1);
        double freq2 = getCenterFreq(channel2);
        double tmp;
        tmp = threeOrderTwoPoint(freq1, freq2);
        if(inBand(tmp)==infchannel) {
            result="两点三阶符合：" +"2*"+getChannel(freq1) + "-" + getChannel(freq2) + "=" + getChannel(tmp)+";";
        }
        tmp = threeOrderTwoPoint(freq2, freq1);
        if(inBand(tmp)==infchannel) {
            result=result+"两点三阶符合："+"2*" + getChannel(freq2) + "-" + getChannel(freq1) + "=" + getChannel(tmp)+";";
        }
        tmp = fiveOrderTwoPoint(freq1, freq2);
        if(inBand(tmp)==infchannel) {
            result=result+"两点五阶符合："+"3*" + getChannel(freq1) + "-2*" + getChannel(freq2) + "=" + getChannel(tmp)+";";
        }
        tmp = fiveOrderTwoPoint(freq2, freq1);
        if(inBand(tmp)==infchannel) {
            result=result+"两点五阶符合："+"3*" + getChannel(freq2) + "-2*" + getChannel(freq1) + "=" + getChannel(tmp)+";";
        }
        return result;
    }
    //两点下行
    public String getPossibleInfChanneldown(int channel1, int channel2,int infchannel) {
        String result ="";
        double freq1 = getCenterFreqdown (channel1);
        double freq2 = getCenterFreqdown (channel2);
        double tmp;
        tmp = threeOrderTwoPoint(freq1, freq2);
        if(inBanddown(tmp)==infchannel) {
            result="两点三阶符合：" +"2*"+getChanneldown(freq1) + "-" + getChanneldown(freq2) + "=" + getChanneldown(tmp)+";";
        }
        tmp = threeOrderTwoPoint(freq2, freq1);
        if(inBanddown(tmp)==infchannel) {
            result=result+"两点三阶符合："+"2*" + getChanneldown(freq2) + "-" + getChanneldown(freq1) + "=" + getChanneldown(tmp)+";";
        }
        tmp = fiveOrderTwoPoint(freq1, freq2);
        if(inBanddown(tmp)==infchannel) {
            result=result+"两点五阶符合："+"3*" + getChanneldown(freq1) + "-2*" + getChanneldown(freq2) + "=" + getChanneldown(tmp)+";";
        }
        tmp = fiveOrderTwoPoint(freq2, freq1);
        if(inBanddown(tmp)==infchannel) {
            result=result+"两点五阶符合："+"3*" + getChanneldown(freq2) + "-2*" + getChanneldown(freq1) + "=" + getChanneldown(tmp)+";";
        }
        return result;
    }

    public double threeOrderTwoPoint(double freq1, double freq2) {
        return 2.0 * freq1 - freq2;
    }
    public double fiveOrderTwoPoint(double freq1, double freq2) {
        return 3.0 * freq1 - 2.0 * freq2;
    }
    
    //三点上行
    public String getPossibleInfChannel(int channel1, int channel2, int channel3,int infchannel) {
        String result ="";
        String result1="";
        String result2="";
        double freq1 = getCenterFreq(channel1);
        double freq2 = getCenterFreq(channel2);
        double freq3 = getCenterFreq(channel3);
        double freq4 = getCenterFreq(infchannel);   
        result1=getAllThreeOrderThreePoint(freq1, freq2, freq3,freq4);
        result2=getAllFiveOrderThreePoint(freq1, freq2, freq3,freq4);
        result=result1+result2;
        return result;
    }
    //三点下行
    public String getPossibleInfChanneldown(int channel1, int channel2, int channel3,int infchannel) {
        String result ="";
        String result1="";
        String result2="";
        double freq1 = getCenterFreqdown(channel1);
        double freq2 = getCenterFreqdown(channel2);
        double freq3 = getCenterFreqdown(channel3);
        double freq4 = getCenterFreqdown(infchannel);   
        result1=getAllThreeOrderThreePointdown(freq1, freq2, freq3,freq4);
        result2=getAllFiveOrderThreePointdown(freq1, freq2, freq3,freq4);
        result=result1+result2;
        return result;
    }
    //三点上行处理
    public String getAllThreeOrderThreePoint(double freq1, double freq2, double freq3,double freq4) {
        String result="";
        double tmp;
        tmp = threeOrderThreePoint(freq1, freq2, freq3);
        if(tmp==freq4) {
            result="三点三阶符合："+getChannel(freq1) + "+" + getChannel(freq2) + "-" + getChannel(freq3) + "=" + getChannel(tmp)+";";
        }
        tmp = threeOrderThreePoint(freq1, freq3, freq2);
        if(tmp==freq4) {
            result=result+"三点三阶符合："+getChannel(freq1) + "+" + getChannel(freq3) + "-" + getChannel(freq2) + "=" + getChannel(tmp)+";";
        }
        tmp = threeOrderThreePoint(freq2, freq3, freq1);
        if(tmp==freq4) {
            result=result+"三点三阶符合："+getChannel(freq2) + "+" + getChannel(freq3) + "-" + getChannel(freq1) + "=" + getChannel(tmp)+";";
        }
        return result;
    }
    public String getAllFiveOrderThreePoint(double freq1, double freq2, double freq3,double freq4) {
        String result ="";
        double tmp;
        tmp = fiveOrderThreePoint1(freq1, freq2, freq3);
        if(tmp==freq4) {
            result="三点五阶符合："+"3*" + getChannel(freq1) + "-" + getChannel(freq2) + "-" + getChannel(freq3) + "=" + getChannel(tmp)+";";
        }
        tmp = fiveOrderThreePoint1(freq2, freq1, freq3);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"3*" + getChannel(freq2) + "-" + getChannel(freq1) + "-" + getChannel(freq3) + "=" + getChannel(tmp)+";";
        }
        tmp = fiveOrderThreePoint1(freq3, freq1, freq2);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"3*" + getChannel(freq3) + "-" + getChannel(freq1) + "-" + getChannel(freq2) + "=" + getChannel(tmp)+";";
        }
        
        tmp = fiveOrderThreePoint2(freq1, freq2, freq3);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChannel(freq1) + "-2" + getChannel(freq2) + "+" + getChannel(freq3) + "=" + getChannel(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq1, freq3, freq2);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChannel(freq1) + "-2" + getChannel(freq3) + "+" + getChannel(freq2) + "=" + getChannel(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq2, freq1, freq3);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChannel(freq2) + "-2" + getChannel(freq1) + "+" + getChannel(freq3) + "=" + getChannel(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq2, freq3, freq1);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChannel(freq2) + "-2" + getChannel(freq3) + "+" + getChannel(freq1) + "=" + getChannel(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq3, freq1, freq2);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChannel(freq3) + "-2" + getChannel(freq1) + "+" + getChannel(freq2) + "=" + getChannel(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq3, freq2, freq1);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChannel(freq3) + "-2" + getChannel(freq2) + "+" + getChannel(freq1) + "=" + getChannel(tmp)+";";
        }
        return result;
    }
    //三点下行处理
    public String getAllThreeOrderThreePointdown(double freq1, double freq2, double freq3,double freq4) {
        String result="";
        double tmp;
        tmp = threeOrderThreePoint(freq1, freq2, freq3);
        if(tmp==freq4) {
            result="三点三阶符合："+getChanneldown(freq1) + "+" + getChanneldown(freq2) + "-" + getChanneldown(freq3) + "=" + getChanneldown(tmp)+";";
        }
        tmp = threeOrderThreePoint(freq1, freq3, freq2);
        if(tmp==freq4) {
            result=result+"三点三阶符合："+getChanneldown(freq1) + "+" + getChanneldown(freq3) + "-" + getChanneldown(freq2) + "=" + getChanneldown(tmp)+";";
        }
        tmp = threeOrderThreePoint(freq2, freq3, freq1);
        if(tmp==freq4) {
            result=result+"三点三阶符合："+getChanneldown(freq2) + "+" + getChanneldown(freq3) + "-" + getChanneldown(freq1) + "=" + getChanneldown(tmp)+";";
        }
        return result;
    }
    public String getAllFiveOrderThreePointdown(double freq1, double freq2, double freq3,double freq4) {
        String result ="";
        double tmp;
        tmp = fiveOrderThreePoint1(freq1, freq2, freq3);
        if(tmp==freq4) {
            result="三点五阶符合："+"3*" + getChanneldown(freq1) + "-" + getChanneldown(freq2) + "-" + getChanneldown(freq3) + "=" + getChanneldown(tmp)+";";
        }
        tmp = fiveOrderThreePoint1(freq2, freq1, freq3);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"3*" + getChanneldown(freq2) + "-" + getChanneldown(freq1) + "-" + getChanneldown(freq3) + "=" + getChanneldown(tmp)+";";
        }
        tmp = fiveOrderThreePoint1(freq3, freq1, freq2);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"3*" + getChanneldown(freq3) + "-" + getChanneldown(freq1) + "-" + getChanneldown(freq2) + "=" + getChanneldown(tmp)+";";
        }
        
        tmp = fiveOrderThreePoint2(freq1, freq2, freq3);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChanneldown(freq1) + "-2" + getChanneldown(freq2) + "+" + getChanneldown(freq3) + "=" + getChanneldown(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq1, freq3, freq2);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChanneldown(freq1) + "-2" + getChanneldown(freq3) + "+" + getChanneldown(freq2) + "=" + getChanneldown(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq2, freq1, freq3);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChanneldown(freq2) + "-2" + getChanneldown(freq1) + "+" + getChanneldown(freq3) + "=" + getChanneldown(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq2, freq3, freq1);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChanneldown(freq2) + "-2" + getChanneldown(freq3) + "+" + getChanneldown(freq1) + "=" + getChanneldown(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq3, freq1, freq2);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChanneldown(freq3) + "-2" + getChanneldown(freq1) + "+" + getChanneldown(freq2) + "=" + getChanneldown(tmp)+";";
        }
        tmp = fiveOrderThreePoint2(freq3, freq2, freq1);
        if(tmp==freq4) {
            result=result+"三点五阶符合："+"2*" + getChanneldown(freq3) + "-2" + getChanneldown(freq2) + "+" + getChanneldown(freq1) + "=" + getChanneldown(tmp)+";";
        }
        return result;
    }

    public double threeOrderThreePoint(double freq1, double freq2, double freq3) {
        return freq1 + freq2 - freq3;
    }
    
    public double fiveOrderThreePoint1(double freq1, double freq2, double freq3) {
        return 3.0 * freq1 - freq2 - freq3;
    }
    
    public double fiveOrderThreePoint2(double freq1, double freq2, double freq3) {
        return 2.0 * freq1 - 2.0 * freq2 + freq3;
    }
    
    
    //上行
    public double getCenterFreq(int channel) {
        double leftBound = 884.6;
        if(channel < 997) {
            channel += 1024;
        }
        double result = leftBound + (double)(channel - 997) * 0.2;
//        System.out.println("" + result);
        return result;
    }
    //
    public double getCenterFreqdown(int channel) {
        double leftBound = 884.6;
        if(channel < 997) {
            channel += 1024;
        }
        double result = leftBound + (double)(channel - 997) * 0.2+45;
//        System.out.println("" + result);
        return result;
    }
    //返回信道号
    public int inBand(double freq) {
        return getChannel(freq);
    }
    
    public int inBanddown(double freq) {
        return getChanneldown(freq);
    }
    
    /**
     * 4M内的频道
     * @param freq
     * @return 
     */
    public int getChannel(double freq) {
        int leftBound = 885 * 10;
        int freqInt = (int)Math.round(freq *  10);//为了解决0.2的计算不精确的问题
        int cnt = (freqInt - leftBound)/2;
//        System.out.println("2的" + cnt + "倍");
        int result = 999 + cnt;
//        System.out.println("频道：" + result);
        return result % 1024;
    }
    public int getChanneldown(double freq) {
        int leftBound = 930 * 10;
        int freqInt = (int)Math.round(freq *  10);//为了解决0.2的计算不精确的问题
        int cnt = (freqInt - leftBound)/2;
//        System.out.println("2的" + cnt + "倍");
        int result = 999 + cnt;
//        System.out.println("频道：" + result);
        return result % 1024;
    }

}
